package indi.zhang.rocketmq.listener;

import com.alibaba.fastjson.JSONObject;
import indi.zhang.mq.MessageContent;
import indi.zhang.mq.api.Consumer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQListener;

import java.nio.charset.StandardCharsets;

@Slf4j
public abstract class BaseListener implements RocketMQListener<Message>, Consumer {
    @Override
    public void onMessage(Message message) {
        try {
            byte[] body =  message.getBody();
            String s = new String(body, StandardCharsets.UTF_8);
            log.info("待消费的区划消息内容是：{}",s);
            JSONObject jsonObject = JSONObject.parseObject(s);
            String messageType = jsonObject.getString("messageType");

            String data = jsonObject.getString("payload");
            MessageContent dataObj = JSONObject.parseObject(data,MessageContent.class);
            log.info("messageType:{},data:{}",messageType,dataObj);
            this.deal(dataObj);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

}
